package com.woniu.darryringmall.admin.config;

import com.woniu.darryringmall.utils.ResponseData;
import com.woniu.darryringmall.utils.ResponseUtils;
import com.woniu.darryringmall.utils.ResultStateCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.ServletException;

/**
 * 统一处理controller的异常
 */
@Slf4j
@RestControllerAdvice
public class ExceptionAdvice {
    @ExceptionHandler
    public ResponseData exceptionHandler(Exception e) {
        e.printStackTrace();
        if (e instanceof ServletException) {
            return ResponseUtils.getResponseData(ResultStateCode.FAIL, "操作失败", e.getMessage());
        } else {
            return ResponseUtils.getResponseData(ResultStateCode.ACCESS, "操作失败", "系统维护中");
        }
    }
}